package com.sky.controller.user;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController("userDishController")
@Api(tags = "菜品浏览相关接口")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 浏览指定分类的菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation(value = "浏览指定分类的菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> getDishByCategoryId(DishDTO dishDTO) {
        log.info("getDishByCategoryId:{}", dishDTO.getCategoryId());
        //先去Redis里找
        String key = "dish_" + dishDTO.getCategoryId();
        List<DishVO> dishVOList = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if(dishVOList != null && dishVOList.size() > 0) {
            return Result.success(dishVOList);
        }

        dishVOList = dishService.getDishWithFlavorByCategoryId(dishDTO);
        //存入Redis中
        redisTemplate.opsForValue().set(key, dishVOList);
        return Result.success(dishVOList);
    }

}
